package com.fzsf.drawingboard.utils;

import android.app.ActivityManager;
import android.content.Context;
import android.support.annotation.DimenRes;
import android.util.Log;
import android.view.WindowManager;

import com.fzsf.drawingboard.app.MyApplication;

import java.util.List;


/**
 * <pre>
 * ScreenUtil
 * </pre>
 *
 * @author lizw
 * @version 1.0.0
 * @since jdk 1.7
 */

final public class ScreenUtils {
    private static final float mDensity = MyApplication.getInstance().getResources().getDisplayMetrics().density;
    private static final float mScaledDensity = MyApplication.getInstance().getResources().getDisplayMetrics().scaledDensity;
    @SuppressWarnings("deprecation")
    private static final int mWidth = ((WindowManager) MyApplication.getInstance().getSystemService(Context.WINDOW_SERVICE)).getDefaultDisplay().getWidth();
    @SuppressWarnings("deprecation")
    private static final int mHeight = ((WindowManager) MyApplication.getInstance().getSystemService(Context.WINDOW_SERVICE)).getDefaultDisplay().getHeight();

    public static float getDensity() {
        return mDensity;
    }

    public static float getScaledDensity() {
        return mScaledDensity;
    }

    public static int getWidth() {
        return mWidth;
    }

    public static int getHeight() {
        return mHeight;
    }

    public static String getScreenPixel() {
        return mWidth + "*" + mHeight;
    }

    public static int Dp2Px(float dp) {
        return (int) (dp * mDensity + 0.5f);
    }

    public static int Px2Dp(int px) {
        return (int) (px / mDensity + 0.5f);
    }

    public static int Sp2Px(float sp) {
        return (int) (sp * mScaledDensity + 0.5f);
    }

    public static int Px2Sp(float px) {
        return (int) (px / mScaledDensity + 0.5f);
    }


    public static int getDimension(@DimenRes int id) {
        float dimension = MyApplication.getInstance().getResources().getDimension(id);
        return (int) dimension;
    }

    public static int getStatusHeight(Context context) {
        int statusHeight = -1;
        try {
            Class<?> clazz = Class.forName("com.android.internal.R$dimen");
            Object object = clazz.newInstance();
            int height = Integer.parseInt(clazz.getField("status_bar_height")
                    .get(object).toString());
            statusHeight = context.getResources().getDimensionPixelSize(height);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return statusHeight;
    }

    /**
     * 判断应用是否在后台
     *
     * @param context
     * @return
     */
    public static boolean isBackground(Context context) {
        ActivityManager activityManager = (ActivityManager) context.getSystemService(Context.ACTIVITY_SERVICE);
        List<ActivityManager.RunningAppProcessInfo> appProcesses = activityManager.getRunningAppProcesses();
        for (ActivityManager.RunningAppProcessInfo appProcess : appProcesses) {
            if (appProcess.processName.equals(context.getPackageName())) {
                if (appProcess.importance == ActivityManager.RunningAppProcessInfo.IMPORTANCE_BACKGROUND) {
                    Log.i("后台", appProcess.processName);
                    return true;
                } else {
                    Log.i("前台", appProcess.processName);
                    return false;
                }
            }
        }
        return false;
    }

    /**
     * 获取dimens
     *
     * @param dimensId 资源id
     * @return
     */
    public static int getDimens(int dimensId) {
        return (int) MyApplication.getInstance().getResources().getDimension(dimensId);
    }
}
